package org.dk.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.dk.mapper.TbSeckillGoodsMapper;
import org.dk.pojo.TbSeckillGoods;
import org.dk.pojo.TbSeckillGoodsExample;
import org.dk.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Date;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class SeckillServiceImpl implements SeckillService {

    @Autowired
    private TbSeckillGoodsMapper tbSeckillGoodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<TbSeckillGoods> selectSeckill() {

        //先查缓存
        List<TbSeckillGoods> list =  (List<TbSeckillGoods>)redisTemplate.boundHashOps("seckillGoods").values();
        if(list == null || list.size() == 0){
            TbSeckillGoodsExample example = new TbSeckillGoodsExample();
            TbSeckillGoodsExample.Criteria criteria = example.createCriteria();
            //库存大于0的
            criteria.andStockCountGreaterThan(0);
            //审核状态
            criteria.andStatusEqualTo("1");
            //秒杀时间在开始和结束之间,当前时间
            Date date = new Date();
            //大于当前时间
            criteria.andStartTimeLessThanOrEqualTo(date);
            //当前时间小于结束时间
            criteria.andEndTimeGreaterThanOrEqualTo(date);
            list = tbSeckillGoodsMapper.selectByExample(example);
            for (TbSeckillGoods tbSeckillGoods : list) {
                redisTemplate.boundHashOps("seckillGoods").put(tbSeckillGoods.getId(),tbSeckillGoods);
            }
        }

        return list;
    }
}
